﻿using System;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

 

namespace mojoPortal.Web.RS.SBM
{
    public partial class RSCallDetail : RSBasePage
    {
        int _callid = 0;
        int _companyid = 0;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                InitializeReadyServicePage();
                SetRSPageHeader("Service Call Detail");

                RSUserIdentity userIdentity = GetRSUserIdentity();
                _companyid = userIdentity.RSCompanyId;

                if (!String.IsNullOrEmpty(Request.QueryString["callid"]))
                    _callid = Convert.ToInt32(Request.QueryString["callid"].ToString());

                
                if (_companyid > 0)
                {
                    bool bHasCall = ucCallDetailControl.LoadCallDataByID(_companyid, _callid);

                    if (bHasCall)
                        ucLocationCallListControl.LoadLocationCallListData(_companyid, _callid);

                    hfSelectedCallId.Value = _callid.ToString();
                }
            }
            
        }
        private void RemoveLocationFromCall(int companyid, int locationid, int callid)
        {
            if (companyid > 0 && locationid > 0 && callid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@locationid", SqlDbType.Int, 0, ParameterDirection.Input, locationid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@callid", SqlDbType.Int, 0, ParameterDirection.Input, callid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblCallLocation WHERE CallId=@callid AND  LocationId=@locationid AND CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                Response.Redirect(Request.RawUrl);
            }
        }
        protected void btnAddLocation_Click(object sender, EventArgs e)
        {

            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                _companyid = userIdentity.RSCompanyId;
                if (!String.IsNullOrEmpty(hfSelectedCallId.Value))
                    _callid = Convert.ToInt32(hfSelectedCallId.Value);
                if (_companyid > 0 && _callid > 0)
                {
                    Response.Redirect("~/RS/SBM/RSManageLocation.aspx?action=add&callid=" + _callid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
    }
}


